<template>
  <div>
    <sticky-top>
      <div class="title">
        <span>{{ isCreate ? '创建网关信息' : '更新网关信息' }}</span>
        <span class="back" @click="back"> <i class="iconfont icon-fanhui"></i> 返回 </span>
        <el-divider></el-divider>
      </div>
    </sticky-top>
    <div class="container">
      <div class="wrap">
        <el-row>
          <el-col :lg="16" :md="20" :sm="24" :xs="24">
            <el-form :model="form" status-icon ref="form" label-width="100px" @submit.native.prevent>
              <el-form-item label="网关名称" prop="name">
                <el-input size="medium" v-model="form.name" placeholder="请填写网关名称"></el-input>
              </el-form-item>
              <el-form-item label="网关唯一码" prop="uuid">
                <el-input size="medium" v-model="form.uuid" placeholder="请填写网关唯一码"></el-input>
              </el-form-item>
              <el-form-item label="放置位置" prop="location">
                <el-input size="medium" v-model="form.location" placeholder="请填写放置位置"></el-input>
              </el-form-item>
              <el-form-item class="submit">
                <el-button
                  v-permission="{ permission: ['创建网关', '更新网关'], type: 'disabled' }"
                  type="primary"
                  @click="submitForm('form')"
                  >保 存</el-button
                >
                <el-button @click="resetForm('form')">重 置</el-button>
              </el-form-item>
            </el-form>
          </el-col>
        </el-row>
      </div>
    </div>
  </div>
</template>

<script>
import Gateway from '@/model/ag_gateway'

export default {
  components: {
  },
  props: {
    isCreate: {
      type: Boolean,
      default: false,
    },
    gatewayId: {
      type: String,
      default: null,
    },
  },
  data() {
    return {
      form: {
        name: '',
        uuid: '',
        location: '',
      },
    }
  },
  watch: {

  },
  async mounted() {
    if (!this.isCreate) {
      const res = await Gateway.getGateway(this.gatewayId)
      this.form = res
    }
  },
  methods: {
    // eslint-disable-next-line
    async submitForm(formName) {
      const postData = { ...this.form }
      let res
      if (this.isCreate) {
        res = await Gateway.addGateway(postData)
      } else {
        res = await Gateway.editGateway(this.gatewayId, postData)
      }
      this.$message.success(`${res.message}`)
      this.$emit('editClose')
      // if (res.code < window.MAX_SUCCESS_CODE) {
      //   this.$message.success(`${res.message}`)
      //   // this.resetForm(formName)
      //   this.$confirm('是否返回上一页', '提示', {
      //     confirmButtonText: '确定',
      //     cancelButtonText: '取消',
      //     type: 'warning',
      //   }).then(async () => {
      //     this.$emit('editClose')
      //   })
      // }
    },
    // 重置表单
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
    back() {
      this.$emit('editClose')
    },
  },
}
</script>

<style lang="scss" scoped>
.el-divider--horizontal {
  margin: 0;
}

.container {
  .title {
    height: 59px;
    line-height: 59px;
    color: $parent-title-color;
    font-size: 16px;
    font-weight: 500;
    text-indent: 40px;

    .back {
      float: right;
      margin-right: 40px;
      cursor: pointer;
    }
  }

  .wrap {
    padding: 20px;
  }

  .submit {
    float: left;
  }
}

.my-autocomplete {
  li {
    line-height: normal;
    padding: 7px;
    display: inline-flex;
    align-content: space-around;
    .name {
      text-overflow: ellipsis;
      overflow: hidden;
    }
    .id {
      margin-right: 15px;
      font-size: 12px;
      color: #b4b4b4;
    }

    .highlighted .addr {
      color: #ddd;
    }
  }
}
</style>
